package com.gensdai.leliang;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Environment;
import android.text.TextUtils;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.gensdai.leliang.Preferences.PreferencesActivity;
import com.gensdai.leliang.base.BaseApplication;
import com.gensdai.leliang.fragment.ContentTypeFragment;
import com.gensdai.leliang.remoteInterface.URLConfig;
import com.gensdai.leliang.utils.BaseUtils;
import com.gensdai.leliang.utils.PinyinUtils;
import com.gensdai.leliang.utils.PreferencesUtils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.wcdb.support.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cat.ereza.customactivityoncrash.CustomActivityOnCrash;
import cat.ereza.customactivityoncrash.config.CaocConfig;

/**
 * Created by Administrator on 2017/5/23.
 */

public class AppApplication extends BaseApplication {
    public static final String DB_NAME = "leliang.db";
    public static final String ClientType = "android";
    public static Context mContext;
    // 字体
    private static HashMap<String, Typeface> sTypefaces = new HashMap<String, Typeface>();

    public static Context applicationContext;

    //经纬度
    public static String lng;
    public static String lat;


    public static final String APPID = "wx701d441207f210af";

    public static IWXAPI msgApi;

    @Override
    public void onCreate() {
        super.onCreate();
        //Install CustomActivityOnCrash   crash处理必须首先初始化
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
                .enabled(true) //default: true    //状态开启
                .showErrorDetails(true) //default: true  显示异常信息
                .showRestartButton(false) //default: true
                .trackActivities(true) //default: false
                .minTimeBetweenCrashesMs(2000) //default: 3000
                .errorDrawable(R.mipmap.app_logo) //default: bug image
                .apply();
        CustomActivityOnCrash.install(this);

        Fresco.initialize(this);

        applicationContext = getApplicationContext();

        msgApi = WXAPIFactory.createWXAPI(this, AppApplication.APPID);
        msgApi.registerApp(AppApplication.APPID);


        if (BuildConfig.DEBUG) {
            String url = PreferencesUtils.getStringPreference(getContext(), AppApplication.PREFERENCE_NAME, PreferencesActivity.prefer1.CURRENTPREFRE, "");
            String picurl = PreferencesUtils.getStringPreference(getContext(), AppApplication.PREFERENCE_NAME, PreferencesActivity.prefer1.CURRENTPREFRE_PIC, "");
            if (!TextUtils.isEmpty(url)) {
                URLConfig.BASEURL = url;
            }
            if (!TextUtils.isEmpty(picurl)) {
                URLConfig.BASEDOMAIN = picurl;
            }
            android.util.Log.d("AppApplication", URLConfig.BASEURL   + "---" + URLConfig.BASEDOMAIN);
        }


    }


    @Override
    public void initDB() {
        DATABASE_NAME = DB_NAME;
    }

    @Override
    public void initEnv() {
// 用pinyin4j来获取对应的应用名
        if ("zh".equals(BaseUtils.getDefaultLanguage())) {
            mAppName = PinyinUtils.chineneToSpell(getResources().getString(
                    R.string.app_name0));
        } else {
            mAppName = PinyinUtils.chineneToSpell(getResources().getString(
                    R.string.app_name0));
        }

        // PREFERENCE_NAME是APP的名称
        PREFERENCE_NAME = mAppName;
        // 判断是否有SD卡再执行创建文件夹的操作
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {

            // 创建应用缓存路径
            File dataDir = new File(Environment.getExternalStorageDirectory()
                    .getPath()
                    + File.separator
                    + mAppName
                    + File.separator
                    + "config" + File.separator);
            if (dataDir.exists()) {
                mSdcardCache = dataDir.getAbsolutePath();
            } else {
                if (dataDir.mkdirs()) {
                    mSdcardCache = dataDir.getAbsolutePath();
                }
            }

            // 创建APP应用下载路径
            mAppDownloads = File.separator + mAppName + File.separator
                    + "download";
            File f = new File(Environment.getExternalStorageDirectory()
                    .getPath() + mAppDownloads);
            if (!f.exists()) {
                f.mkdirs();
            }

            // 图片的缓存路径
            File imageCache = new File(dataDir.toString() + File.separator
                    + "imageCache" + File.separator);
            if (imageCache.exists()) {
                mSdcardImageCache = imageCache.getAbsolutePath();
            } else {
                if (imageCache.mkdirs()) {
                    mSdcardImageCache = imageCache.getAbsolutePath();
                }
            }
            // 保存拍照后的图片
            File imageCamera = new File(dataDir.toString() + File.separator
                    + "imageCamera" + File.separator);
            if (imageCamera.exists()) {
                mSdcardImageCamera = imageCamera.getAbsolutePath();
            } else {
                if (imageCamera.mkdirs()) {
                    mSdcardImageCamera = imageCamera.getAbsolutePath();
                }
            }
        }

//        Log.e("cwj", "外置SD卡路径 = " + BaseUtils.getExtendedMemoryPath(getContext()));
//        Log.e("cwj", "内置SD卡路径 = " + Environment.getExternalStorageDirectory().getAbsolutePath());
//        Log.e("cwj", "手机内存根目录路径  = " + Environment.getDataDirectory().getParentFile().getAbsolutePath());


        //初始化menu菜单栏
//        menu = new ArrayList<>();
//        MainMenu baobao = new MainMenu();
//        baobao.setId(ContentTypeFragment.HOT_1);
//        baobao.setResId(R.mipmap.menu_baobao);
//        baobao.setName(getString(R.string.home_yuntong));
//
//
//        MainMenu wanju = new MainMenu();
//        wanju.setId(ContentTypeFragment.HOT_2);
//        wanju.setResId(R.mipmap.menu_wanju);
//        wanju.setName(getString(R.string.home_nongchanp));
//
//        MainMenu yingtong = new MainMenu();
//        yingtong.setId(ContentTypeFragment.HOT_3);
//        yingtong.setResId(R.mipmap.menu_yingtong);
//        yingtong.setName(getString(R.string.home_yizhiwanju));
//
//
//        MainMenu lema = new MainMenu();
//        lema.setId(ContentTypeFragment.HOT_4);
//        lema.setResId(R.mipmap.menu_lema);
//        lema.setName(getString(R.string.home_riyongpn));
//
//        MainMenu nongchanp = new MainMenu();
//        nongchanp.setId(ContentTypeFragment.HOT_5);
//        nongchanp.setResId(R.mipmap.menu_nongchanpin);
//        nongchanp.setName(getString(R.string.home_meizhuang));
//
//        MainMenu riyongp = new MainMenu();
//        riyongp.setId(ContentTypeFragment.HOT_6);
//        riyongp.setResId(R.mipmap.menu_riyongp);
//        riyongp.setName(getString(R.string.home_hufu));
//
//        menu.add(baobao);
//        menu.add(wanju);
//        menu.add(yingtong);
//        menu.add(lema);
//        menu.add(nongchanp);
//        menu.add(riyongp);


    }
}
